package de.sendorian.app.forumArchive.domain;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import de.sendorian.util.io.FileWriter;

@Entity
public class Archive extends File {

	@OneToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "ARCHIVES_IMAGES")
	@Fetch(FetchMode.SUBSELECT)
	private List<Image> images = new ArrayList<Image>();

	public List<Image> getImages() {
		return images;
	}


	public void setImages(List<Image> images) {
		this.images = images;
	}

	public List<String> deflate() throws IOException {
		List<String> imagesInArchive = new FileWriter().deflateArchiveFlat(getLocalPath(), getName());
		for (String imageInArchive : imagesInArchive) {
			images.add(new Image(imageInArchive));
		}

		return imagesInArchive;
	}

}
